package com.qiao.startapp.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.qb.common.router.RouterPath
import com.qiao.startapp.R
import com.qiao.startapp.databinding.AppActivityMainBinding
import com.qiao.startapp.ui.viewmodel.MainViewModel
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.qb.common.base.CommonBaseActivity
import com.qb.common.utils.UtilParams
import com.qb.main.ui.fragment.MainFragment
import com.qb.square.ui.fragment.SquareFragment
import com.qiao.startapp.MyApplication
import kotlin.system.exitProcess


@Route(path = RouterPath.App.URL_App_MainActivity)
open class MainActivity : CommonBaseActivity<MainViewModel, AppActivityMainBinding>(), View.OnClickListener{


    private var exitTime: Long = 0
    private var mainFragment:MainFragment?=null
    private var squareFragment: SquareFragment?=null

    override fun layoutId(): Int = R.layout.app_activity_main

    override fun initView(savedInstanceState: Bundle?) {
        mainFragment = MainFragment.get()
        squareFragment = SquareFragment.get()
        mBinding.tvMain.setOnClickListener(this)
        mBinding.tvMy.setOnClickListener(this)
        replace(0)
    }
    fun replace(type:Int){
        if (type==0){
            supportFragmentManager.beginTransaction().replace(mBinding.llOut.id,mainFragment as Fragment).commit()
            mBinding.tvMain.setTextColor(Color.parseColor("#BB996C"))
            mBinding.tvMy.setTextColor(Color.parseColor("#333333"))
        }else if (type==1){
            supportFragmentManager.beginTransaction().replace(mBinding.llOut.id,squareFragment as Fragment).commit()
            mBinding.tvMain.setTextColor(Color.parseColor("#333333"))
            mBinding.tvMy.setTextColor(Color.parseColor("#BB996C"))
        }
    }
    override fun initData() {
//        viewModel.phoneQuery(UtilParams.addParamsEmpty()).observe(this,{
//            LogUtils.e("==-->common data:=$it")
//        })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onClick(view: View?) {
        view?.let {
            when(view.id){
                mBinding.tvMain.id->{
                    replace(0)
                }
                mBinding.tvMy.id->{
                    replace(1)
                }
                else -> {}
            }
        }

    }
    private fun exit() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ToastUtils.showShort("再按一次退出程序")
            exitTime = System.currentTimeMillis()
        } else {
            finish()
            exitProcess(0)
        }
    }
}